﻿{{~ queryDto = entity_type + "QueryDto" ~}}
{{~ for using_item in usings ~}}
using {{ using_item }};
{{~ end ~}}

namespace {{ namespace }}
{
    /// <summary>
    /// {{ class_name }} 控制器
    /// </summary>
    public class {{ class_name }} : ControllerBase
    {
        private readonly {{ interface_name }} _service;

        /// <summary>
        /// 构造函数，注入服务
        /// </summary>
        /// <param name="service">服务接口</param>
        public {{ class_name }}({{ interface_name }} service)
        {
            _service = service;
        }

        /// <summary>
        /// 获取所有{{ entity_type }}对象
        /// </summary>
        /// <returns>返回{{ entity_type }}列表</returns>
        [HttpGet]
        public PageResponse<{{ return_type }}> GetList([FromQuery] {{ queryDto }} query)
        {
            return _service.GetPageList(query);
        }

        /// <summary>
        /// 根据ID获取{{ entity_type }}对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <returns>返回指定的{{ entity_type }}对象</returns>
        [HttpGet("{id}")]
        public {{ return_type }} GetById([FromRoute] {{ id_type }} id)
        {
            var result = _service.GetById(id);
            return result ?? throw new KeyNotFoundException($"未找到ID为 {id} 的{{ entity_type }}对象。");
        }

        /// <summary>
        /// 创建新的{{ entity_type }}对象
        /// </summary>
        /// <param name="entity">要创建的{{ entity_type }}对象</param>
        /// <returns>返回创建后的{{ entity_type }}对象</returns>
        [HttpPost]
        public {{ return_type }} Create([FromBody] {{ entity_type }} entity)
        {
            _service.Create(entity);
            return entity; // 直接返回创建的实体
        }

        /// <summary>
        /// 更新现有的{{ entity_type }}对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <param name="entity">更新后的{{ entity_type }}对象</param>
        [HttpPut]
        public void Update([FromBody] {{ entity_type }} entity)
        {
            _service.Update(entity);
        }

        /// <summary>
        /// 批量删除{{ entity_type }}对象
        /// </summary>
        /// <param name="ids">对象的IDS</param>
        [HttpDelete]
        public void Delete([FromBody] {{ id_type }}[] ids)
        {
             _service.Delete(ids);
        }
        
        /// <summary>
        /// 单个删除{{ entity_type }}对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        [HttpDelete("{id}")]
        public void Delete([FromRoute] {{ id_type }} id)
        {
            _service.Delete(id);
        }
    }
}
